(function($){
	
	if(!$.ajax.isOverride) {
		var ajax_override = $.ajax;
		$.ajax=function(s){
			if(0>s.url.indexOf("http"))
				s.url = CONTEXT_PATH + s.url;
			return ajax_override(s);
		}
	}
	
	/**
	 * 获取选中区域中的表单元素值，返回JSON对象
	 */
	 $.fn.getParamJson = function() {
		var json = {};
		var jsonHandle = function() {
			var $el = $(this);
			if($el.is(':disabled')) { return true; }
			if($el.is(':submit,:image,:reset,:button')) { return true; }
			var name = jQuery.trim($el.attr('name'));
			if(name == '') { name = jQuery.trim($el.attr('id')); }
			if(name == '') { return true; }
			var value = json[name];
			if(typeof(value) == 'undefined') {
				if($el.is(':radio:checked,:checkbox:checked,:text,:file,:password,textarea,input[type="hidden"]')) { json[name] = $el.val();}
				if($el.is('select')) {
					if($el.attr('multiple') == 'multipe') {
						json[name] = {};
						$el.find('option:selected').each(function() {
							json[name].push($(this).val());
						});
					} else json[name] = $el.find('option:selected').val();
				}
			} else if (jQuery.isArray(value)) {
				if($el.is(':checkbox:checked,:text,:file,:password,textarea,input[type="hidden"]')) { json[name].push($el.val()); }
				if($el.is('select')) {
					$el.find('option:selected').each(function(){
						json[name].push($(this).val());
					});
				}
			} else {
				if($el.is(':checkbox:checked,:text,:file,:password,textarea,input[type="hidden"]')) {
					json[name] = [value];
					json[name].push($el.val());
				}
				if($el.is('select')) {
					json[name] = [value];
					$el.find('option:selected').each(function(){
						json[name].push($(this).val());
					});
				}
			}
		};
		this.each(function() {
			var $this = $(this);
			if($this.is(':input')) { jsonHandle.call(this);}
			else { $this.find(':radio:checked,:checkbox:checked,:text,:file,:password,select,textarea,input[type="hidden"]').each(jsonHandle)}
		});
		return json;
	 }
	/**
	 *	本地存储工具
	 */
	$.Storage = function(){
		this.init.apply(this, arguments);
	}
	$.Storage.prototype = {
		dataStorage:null,
		_getLocalStroage:function() {
			if(typeof(localStorage) === "object") { this.dataStorage = localStorage;}
			else if(typeof(globalStorage) === "object") { this.dataStorage = globalStorage[location.host] }
		},
		_getUserData:function() {
			var doc = document,dataNode = doc.createElement("div");
			dataNode.addBehavior("#default#userData");
			doc.body.appendChild(dataNode);
			this.dataStorage || (this.dataStorage = {
				setItem : function(key, value) {
					dataNode.setAttribute(key, value);
					dataNode.save("dataStorage");
				},
				getItem : function(key) {
					dataNode.load("dataStorage");
					return dataNode.getAttribute(key);
				},
				removeItem : function(key) {
					dataNode.removeAttribute(key);
					dataNode.save("dataStorage");
				}
			});
		},
		init:function() {
			this._getLocalStroage();
			this.dataStorage || this._getUserData();
		},
		setItem:function(key, value) {
			this.dataStorage.setItem(key, value);
		},
		getItem:function(key) {
			return this.dataStorage.getItem(key);
		},
		removeItem:function(key) {
			this.dataStorage.removeItem(key);
		}
	}
	/**
	 * 常用工具函数
	 */
	var tools = {
		/**
		 * 获取URL上的参数值
		 */
		request:function() {
			var url = location.href,
				paraString = url.substring(url.indexOf("?")+1,url.length).split("&"),
				paraObj = {}, returnValue;
			for(i = 0; j = paraString[i]; i++) {
				paraObj[j.substring(0,j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=")+1, j.length);
			} 
			returnValue = paraObj[paras.toLowerCase()];
			if(typeof(returnValue) == "undefined") { return ""; }
			else { return returnValue; }
		},
		escapeHtmlTag:function(html) {
			html = html.replace(/</ig,"&lt;");
			html = html.replace(/>/ig,"&gt;");
			html = html.replace(/"/ig,"&quot;");
		},
		login:function(opt) {
			opt = opt || {};
			opt = jQuery.extend({
				container:document.body,		//登录窗体父容器
				url:"/operator_login.do",			//提交的地址
				isCaptcha:false,				//是否开启验证码
				captchaImg:"/authCode.do",		//验证码图片地址
				onBeforeSumbit:function(){},	//提交之前执行的函数
				form:null						//表单对象
			},opt);
			opt.label = jQuery.extend({
				account:"账&nbsp;号:",
				password:"密&nbsp;码:",
				captcha:"验证码:",
				btnSubmit:"登录",
				btnReset:"重置"
			},opt.label);
			var LocalStorage = tools.localStorage,
				ACCOUNT_HIS_KEY = "account_his"+(CONTEXT_PATH || "").replace(/[\/\\]/g,"_")
				_form = opt.form = $("<form></form>").appendTo(opt.container),
				html = ["<table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"],
				isSubmit = false,result = {
					reset : function() {
						$(".checkbox", _form).val("").eq(0).focus();
					},
					submit : function() {
						if(isSubmit) return;
						if(opt.onBeforeSumbit.apply(this, arguments) == false) { return; }
						param = _form.getParamJson();
						login.call(opt, opt.url, param);
					}
				};
			html.push("<tr><td class=\"title\">"+opt.label.account+"</td><td colspan=\"2\"><input type=\"text\" name=\"account\" class=\"checkbox text-user\" tabindex=\"2\"/></td></tr>");
			html.push("<tr><td class=\"title\">"+opt.label.password+"</td><td colspan=\"2\"><input type=\"password\" name=\"password\" class=\"checkbox text-pass\" tabindex=\"3\"/></td></tr>");
			if(opt.isCaptcha) {
				html.push("<tr><td class=\"title\">"+opt.label.captcha+"</td><td class=\"middle\"><input type=\"text\" name=\"authCode\" maxlength=\"4\" class=\"checkbox text-auth\" tipSite=\"bottom\" tabindex=\"4\" autocomplete=\"off\"/></td><td><img src=\""+CONTEXT_PATH+opt.captchaImg+"\" height=\"30\" onclick=\"this.src='${path }/authCode.qt?random='+Math.floor(Math.random()*10000+1)\" title=\"验证码不区分大小写\" style=\"cursor:pointer;\"></td></tr>");
			}
			html.push("<tr><td id=\"errorInfo\" colspan=\"3\"></td></tr>");
			html.push("<tr><td colspan=\"3\" align=\"center\" style=\"height:1%;\"></td></tr>");
			html.push("</table>");
			_form.append(html.join(""));
			$("<a href=\"javascript:void(0)\" class=\"buttom submit\" tabindex=\"10\">"+opt.label.btnSubmit+"</a>").appendTo($("table>tbody>tr:last>td", _form)).click(result.submit);
			$("<a href=\"javascript:void(0)\" class=\"button reset\" tabindex=\"11\">"+opt.label.btnReset+"</a>").appendTo($("table>tbody>tr:last>td", _form)).click(result.reset);
			$(document.body).keypress(function(e){
				if(e.keyCode == 13) result.submit();
			});
			$(document).ready(function(){
				_form.find("input:visible[value=\"\"]:eq(0)").focus();
			});
			function login(url, param){
				_form.find("#errorInfo").html("");
				jQuery.getJSON(url, param, function(data) {
					isSubmit = false;
					if(data && data.b) {
						LocalStorage.setItem(ACCOUNT_HIS_KEY, data.account||"");
						goto_url(CONTEXT_PATH+'/index.jsp')
					}
				});
			}
			function goto_url(url){
				if(/MSIE\s*(\d+\.\d+);/.test(navigator.userAgent)){
					var tmp = document.createElement('a');
					tmp.href = url;
					document.body.appendChild(tmp);
					tmp.click();
				}else location.href = url;
			};
			return result;
		},
		getRandomNum: function(){
			return Math.floor(Math.random()*10000+1);
		}
	};
	window.Tools = tools;
})(jQuery);

jQuery(document).ready(function(){
	Tools.localStorage = new jQuery.Storage();
	$("form").each(function(){
		var url = $(this).attr("action");
		$(this).attr("action", CONTEXT_PATH+url);
	});
})










